<template>
  <view class="page-index">
    <IndexPage :orgInfo="orgInfo" v-if="active === 0" />
    <foodPage v-if="active === 1" />
    <TicketPage v-if="active === 2" />
    <OrderPage :appCodeList="appCodeList" v-if="active === 3" />
    <MyPage :orgInfo="orgInfo" v-if="active === 4" />
    <tabBar
      :active="active"
      :appCodeList="appCodeList"
      @change="changeTab"
    ></tabBar>
  </view>
</template>

<script>
import IndexPage from "../indexPage";
import foodPage from "../foodPage/list";
import TicketPage from "../ticketPage";
import OrderPage from "../orderPage";
import MyPage from "../myPage";
import { getOrgInfo } from "@/api/user.js";
import { foodTableInfo } from "@/api/foodPage.js";
export default {
  components: {
    IndexPage,
    foodPage,
    TicketPage,
    OrderPage,
    MyPage,
  },
  data() {
    return {
      active: 0,
      orgInfo: {},
      appCodeList: [],
      titles: ["首页", "点餐", "购票", "订单", "我的"],
      tableNo: "",
      storeId: "",
    };
  },
  onLoad(options) {
    console.log("【index获取参数】", options);
    if (options.orgId) {
      // 店铺ID
      uni.setStorageSync("storeId", options.orgId);
    }
    this.getOrgInfo(uni.getStorageSync("storeId"));
    if (options.tableId) {
      this.getTableInfoById(options.tableId);
    }
    // #ifdef MP-WEIXIN
    wx.hideHomeButton();
    // #endif
    // const active = uni.getStorageSync("activeTab") || 0;
    // this.active = active;
    // uni.setNavigationBarTitle({
    //   title: this.titles[active] || "",
    // });
    if (options.active) {
      this.changeTab(Number(options.active));
      return;
    }
    this.active = 0;
    uni.setNavigationBarTitle({
      title: "首页",
    });
  },
  methods: {
    changeTab(active) {
      this.active = active;
      uni.setNavigationBarTitle({
        title: this.titles[active] || "",
      });
    },
    async getTableInfoById(id) {
      const { body } = await foodTableInfo({ id });
      console.log(body.orgId, "桌号详情---小程序机构id===");
      uni.setStorageSync("foodOrgId", body.orgId || "");
      this.getOrgInfo(body.onlineOrgId);
      setTimeout(() => {
        uni.navigateTo({
          url: `/pages/foodPage/list?tableNo=${body.tableNo}&storeId=${body.storeId}`,
        });
      }, 100);
    },
    //获取机构详情
    async getOrgInfo(id) {
      const { body } = await getOrgInfo({ id });
      this.orgInfo = body;
      console.log(77777777777, this.orgInfo)
      if (body.offlineOrgId) {
        uni.setStorageSync("foodOrgId", body.offlineOrgId || "");
      }
      if (body.appList) {
        this.appCodeList = body.appList.map((v) => v.appCode);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.page-index {
  min-height: 100%;
  background-color: #fff;
  padding-bottom: 170rpx;
  /* overflow-y: auto; */
}
</style>
